package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class PowerActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_power);
		
		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.power, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/

	//final Button button = (Button) findViewById(R.id.button1);
    // button.setOnClickListener(new View.OnClickListener() {
    	
	public void onClick(View view) {
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			double input = Double.parseDouble(text.getText().toString());
			String unit1 = spin1.getSelectedItem().toString();
			String unit2 = spin2.getSelectedItem().toString();
			double wattvalue;
			double d;
						
			if (unit1.equals("Watt"))
			{
				wattvalue = input;
			}
			
			else if (unit1.equals("Kilowatt"))
			{
				wattvalue = input*(1000);
			}
			
			else if (unit1.equals("Erg / Second"))
			{
				wattvalue = input*(0.0000001);
			}
			
			else if (unit1.equals("Calorie / Second"))
			{
				wattvalue = input*(4.1868);
			}
			
			else if (unit1.equals("Joule / Hour"))
			{
				wattvalue = input*(1/3600d);
			}
			
			else if (unit1.equals("BTU / Second"))
			{
				wattvalue = input*(1055.05585262);
			}
			
			else if (unit1.equals("BTU / Hour"))
			{
				wattvalue = input*(1055.05585262/3600);
			}
			
			else if (unit1.equals("Foot Pound / Second"))
			{
				wattvalue = input*(1.3558179483314004);
			}
			
			else if (unit1.equals("Foot Pound / Hour"))
			{
				wattvalue = input*(1.3558179483314004/3600);
			}
			
			else if (unit1.equals("Litre Atmosphere / Second"))
			{
				wattvalue = input*(101.325);
			}
			
			else if (unit1.equals("Litre Atmosphere / Minute"))
			{
				wattvalue = input*(101.325/60);
			}
			
			else if (unit1.equals("Horsepower"))
			{
				wattvalue = input*(746);
			}
			
			else if (unit1.equals("Poncelet"))
			{
				wattvalue = input*(980.665);
			}
			
			else if (unit1.equals("Meter Kilogram Force / Second"))
			{
				wattvalue = input*(9.80665);
			}
			
			else if (unit1.equals("Lusec"))
			{
				wattvalue = input*(0.0001333224);
			}
			
			else if (unit1.equals("Tons of Refridgeration"))
			{
				wattvalue = input*(3938.875);
			}
			
			else if (unit1.equals("Tons of Air Conditioning"))
			{
				wattvalue = input*(3504);
			}
			
			else
			{
				wattvalue = 0;
			}
			
			
			if (unit2.equals("Watt")){
				d = wattvalue;
			}
			
			else if (unit2.equals("Kilowatt")){
				d = wattvalue*(0.0001);
			}
			
			else if (unit2.equals("Erg / Second")){
				d = wattvalue*(10000000);
			}
			
			else if (unit2.equals("Calorie / Second")){
				d = wattvalue*(1/4.1868);
			}
			
			else if (unit2.equals("Joule / Hour")){
				d = wattvalue*(3600);
			}
			
			else if (unit2.equals("BTU / Second")){
				d = wattvalue*(1/1055.05585262);
			}
			
			else if (unit2.equals("BTU / Hour")){
				d = wattvalue*(3600/1055.05585262);
			}
			
			else if (unit2.equals("Foot Pound / Second")){
				d = wattvalue*(1.3558179483314004);
			}
			
			else if (unit2.equals("Foot Pound / Hour")){
				d = wattvalue*(3600/1.3558179483314004);
			}
			
			else if (unit2.equals("Litre Atmosphere / Second")){
				d = wattvalue*(101.325);
			}
			
			else if (unit2.equals("Litre Atmosphere / Minute")){
				d = wattvalue*(60/101.325);
			}
			
			else if (unit2.equals("Horsepower")){
				d = wattvalue*(1/746d);
			}
			
			else if (unit2.equals("Poncelet")){
				d = wattvalue*(1/980.665);
			}
			
			else if (unit2.equals("Meter Kilogram Force / Second")){
				d = wattvalue*(1/9.80665);
			}
			
			else if (unit2.equals("Lusec")){
				d = wattvalue*(1/0.0001333224);
			}
			
			else if (unit2.equals("Tons of Refridgeration")){
				d = wattvalue*(1/3938.875);
			}
			
			else if (unit2.equals("Tons of Air Conditioning")){
				d = wattvalue*(1/3504d);
			}
			
			else
			{
				d = 0;
			}
		
			result.setText(String.valueOf(d));
			
			resultString.setText(String.valueOf(input) + " " + unit1 + " =");
			resultString1.setText(String.valueOf(d) + " " + unit2);
			
		}
	
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}
}
